package com.health.healthSys.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.health.common.core.domain.entity.SysUser;
import com.health.common.utils.StringUtils;
import com.health.healthSys.domain.HealthSport;
import com.health.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.BusinessType;
import com.health.healthSys.domain.HealthRecords;
import com.health.healthSys.service.IHealthRecordsService;
import com.health.common.utils.poi.ExcelUtil;
import com.health.common.core.page.TableDataInfo;

/**
 * 健康数据Controller
 *
 * @author maple
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/healthSys/records")
public class HealthRecordsController extends BaseController
{
    @Autowired
    private IHealthRecordsService healthRecordsService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询健康数据列表
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthRecords healthRecords)
    {
        startPage();
        List<HealthRecords> list = healthRecordsService.selectHealthRecordsList(healthRecords);
        return getDataTable(list);
    }

    /**
     * 导出健康数据列表
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:export')")
    @Log(title = "健康数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthRecords healthRecords)
    {
        List<HealthRecords> list = healthRecordsService.selectHealthRecordsList(healthRecords);
        ExcelUtil<HealthRecords> util = new ExcelUtil<HealthRecords>(HealthRecords.class);
        util.exportExcel(response, list, "健康数据数据");
    }

    /**
     * 获取健康数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:query')")
    @GetMapping(value = "/{healthId}")
    public AjaxResult getInfo(@PathVariable("healthId") String healthId)
    {
        return success(healthRecordsService.selectHealthRecordsByHealthId(healthId));
    }

    /**
     * 新增健康数据
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:add')")
    @Log(title = "健康数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HealthRecords healthRecords)
    {

        if(healthRecords.getUserId()==null){
            healthRecords.setUserId(getUserId());
            healthRecords.setDeptId(getDeptId());
            healthRecords.setUserName(getUsername());

        }else {
            SysUser sysUser=iSysUserService.selectUserById(healthRecords.getUserId());
            healthRecords.setUserName(sysUser.getUserName());

        }
        return toAjax(healthRecordsService.insertHealthRecords(healthRecords));
    }

    /**
     * 管理员修改健康数据
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:edit')")
    @Log(title = "健康数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HealthRecords healthRecords)
    {
        SysUser sysUser=iSysUserService.selectUserById(healthRecords.getUserId());
        healthRecords.setUserName(sysUser.getUserName());
        return toAjax(healthRecordsService.updateHealthRecords(healthRecords));
    }
    /**
     * 用户修改健康数据
     */
    @PutMapping("/useredit")
    public AjaxResult useredit(@RequestBody HealthRecords healthRecords)
    {

        return toAjax(healthRecordsService.updateHealthRecords(healthRecords));
    }

    /**
     * 删除健康数据
     */
    @PreAuthorize("@ss.hasPermi('healthSys:records:remove')")
    @Log(title = "健康数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{healthIds}")
    public AjaxResult remove(@PathVariable String[] healthIds)
    {
        return toAjax(healthRecordsService.deleteHealthRecordsByHealthIds(healthIds));
    }

    @GetMapping("/bloodSugar")
    @PreAuthorize("@ss.hasPermi('healthSys:records:query')")
    public AjaxResult getBloodSugarData(
            @RequestParam Long userId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        if (userId == null) {
            userId = getUserId(); // 从BaseController继承的方法
        }
        List<HealthRecords> list = healthRecordsService.selectBloodSugarData(userId, startDate, endDate);
        return AjaxResult.success(list);
    }

    @GetMapping("/bloodPressure")
    @PreAuthorize("@ss.hasPermi('healthSys:records:query')")
    public AjaxResult getBloodPressureData(
            @RequestParam Long userId,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        if (userId == null) {
            userId = getUserId(); // 从BaseController继承的方法
        }
        List<HealthRecords> list = healthRecordsService.selectBloodPressureData(userId, startDate, endDate);
        return AjaxResult.success(list);
    }


}
